package com.opengroupe.resources;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opengroupe.entities.AccessCode;
import com.opengroupe.service.common.CommonService;

/**
 * Identification services
 * 
 * @author MMB11447
 *
 */
@Service("accessCodeResource")
@Path("/accessCode")
public class AccessCodeResource {
	
	@Autowired
	private CommonService commonService;

	/**
	 * Asks for a new access code or gets the generated 
	 * access code for the current date.
	 * 
	 * @return the current access code
	 */
	@GET
	@Produces("application/json")
	@Path("/getAccessCode")
	public String accessCode() {
		
		Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
		
		return gson.toJson(commonService.getGeneratedAccessCode(), AccessCode.class);
	}
	
	/**
	 * Allows administrator's identification by comparing the given 
	 * access code to the access code generated for the current date.
	 * 
	 * @param code administrator's access code
	 * @return <code>true</code> if succeeded. <code>false</code> otherwise.
	 */
	@POST
	@Produces("application/json")
	@Path("/identification")
	public String identify(String code) {
		if(code == null || code.equals("null")) {
			return Boolean.toString(false);
		}
		return Boolean.toString(commonService.identification(code));
	}
}
